var express = require('express')
var router = express.Router()
const user = require('../utils')
const query = require('../pool')
const fs = require('fs').promises;

// 获取图片列表
router.get('/getList', async (req, res) => {
    const project = req.query.project;
    const pageNo = req.query.pageNo;
    const pageSize = req.query.pageSize;
    const [err0, data0] = await query(`
        SELECT COUNT(*) AS total FROM HopeTravel.images;
    `)
    const [err, data] = await query(`
        SELECT *
        FROM HopeTravel.images WHERE project = '${project}' LIMIT ${(pageNo - 1) * pageSize}, ${pageSize};
    `)
    if (err) {
        res.send({
        code: -1,
        msg: err
        })
    } else {
        res.send({
        code: 0,
        data: {
            list: data,
            total: data0[0].total
        }
        })
    }
});

// 获取某张图片
router.get('/getDetail', async (req, res) => {
    const name = req.query.name;
    const [err, data] = await query(`
        SELECT *
        FROM HopeTravel.images WHERE name = '${name}';
    `)
    if (err) {
        res.send({
        code: -1,
        msg: err
        })
    } else {
        res.send({
        code: 0,
        data: data.length ? data[0] : null
        })
    }
});

module.exports = router
